package ctrl

import (
	"fmt"
	"im_unitree/args"
	"im_unitree/model"
	"im_unitree/service"
	. "im_unitree/utils"
	"net/http"
)

var contactService service.ContactService

func LoadMe(w http.ResponseWriter, req *http.Request) {
	var arg args.ContactArg
	Bind(req, &arg)
	me := contactService.SearchMe(arg.Userid)
	RespOk(w, me, "")
}

func LoadFriend(w http.ResponseWriter, req *http.Request) {
	var arg args.ContactArg

	Bind(req, &arg)

	users := contactService.SearchFriend(arg.Userid)
	RespOkList(w, users, len(users))
}

func LoadCommunity(w http.ResponseWriter, req *http.Request) {
	var arg args.ContactArg
	//如果这个用的上,那么可以直接
	Bind(req, &arg)
	comunitys := contactService.SearchComunity(arg.Userid)
	RespOkList(w, comunitys, len(comunitys))
}
func JoinCommunity(w http.ResponseWriter, req *http.Request) {
	var arg args.ContactArg

	//如果这个用的上,那么可以直接
	Bind(req, &arg)
	err := contactService.JoinCommunity(arg.Userid, arg.Dstid)
	// 新增了群，需要刷新一下自己的group set
	AddGroupId(arg.Userid, arg.Dstid)
	if err != nil {
		RespFail(w, err.Error())
	} else {
		RespOk(w, nil, "")
	}
}

func CreateCommunity(w http.ResponseWriter, r *http.Request) {
	var arg model.Community
	Bind(r, &arg)
	community, err := contactService.CreateCommunity(arg)
	if err != nil {
		RespFail(w, err.Error())
		fmt.Println(err.Error())
	} else {
		RespOk(w, community, "")
	}

}

func Addfriend(w http.ResponseWriter, req *http.Request) {
	//定义一个参数结构体
	/*request.ParseForm()
	mobile := request.PostForm.Get("mobile")
	passwd := request.PostForm.Get("passwd")
	*/
	var arg args.ContactArg
	Bind(req, &arg)
	//调用service
	err := contactService.AddFriend(arg.Userid, arg.Dstid)
	//
	if err != nil {
		RespFail(w, err.Error())
	} else {
		RespOk(w, nil, "好友添加成功")
	}
}
